package puziran.sofa.http.client;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

public class HttpLoadBalance {
	private Hashtable allClienct = new Hashtable();
	private Collection allHTTPURLs;
	private Collection allErrURLs;
	private Collection allLiveURLs;
	private int refreshSeconds = 60;
	private static long latestCheckTime = 0L;
	private String page600 = "600.html";
	private String page601 = "601.html";
	private String page602 = "602.html";
	private String page603 = "603.html";
	private String page604 = "604.html";

	private static HttpLoadBalance dis = new HttpLoadBalance();

	private HttpLoadBalance() {
		this.allHTTPURLs = new ArrayList();
		this.allErrURLs = new ArrayList();
		int lsize = Integer.parseInt(HttpPropertyUtil.getValue("live.size"));
		int esize = Integer.parseInt(HttpPropertyUtil.getValue("err.size"));
		for (int i = 0; i < lsize; i++) {
			String s = HttpPropertyUtil.getValue("live" + i);
			if ((s != null) && (s.trim().length() != 0)) {
				this.allHTTPURLs.add(s);
			}
		}
		for (int i = 0; i < esize; i++) {
			String s = HttpPropertyUtil.getValue("err" + i);
			if ((s != null) && (s.trim().length() != 0)) {
				this.allErrURLs.add(s);
			}
		}
		this.refreshSeconds = Integer.parseInt(HttpPropertyUtil.getValue("refresh.seconds"));
	}

	public static HttpLoadBalance getInstance() {
		return dis;
	}

	public String route() {
		if (System.currentTimeMillis() - latestCheckTime > this.refreshSeconds * 1000) {
			checkURLs();
		}

		if (this.allLiveURLs.size() == 0) {
			if (this.allErrURLs.size() == 0) {
				return this.page601;
			}
			int random = (int) Math.random() * this.allErrURLs.size();
			String url = (String) this.allErrURLs.toArray()[random];
			return url;
		}
		double rd = Math.random();
		System.out.println("rd=" + rd);
		int random = (int) Math.floor(rd * this.allLiveURLs.size());
		String url = (String) this.allLiveURLs.toArray()[random];
		if (isLive(url)) {
			System.out.println("调用的服务Url=" + url);
			return url;
		}
		checkURLs();
		if (this.allLiveURLs.size() == 0) {
			random = (int) Math.random() * this.allErrURLs.size();
			url = (String) this.allErrURLs.toArray()[random];
			return url;
		}

		random = (int) Math.random() * this.allLiveURLs.size();
		url = (String) this.allLiveURLs.toArray()[random];
		System.out.println("调用的服务Url=" + url);
		return url;
	}

	public static void main(String[] args) {
		String url = getInstance().route();
		System.out.println(url);
	}

	private void checkURLs() {
		this.allLiveURLs = new ArrayList();
		Iterator it = this.allHTTPURLs.iterator();
		while (it.hasNext()) {
			String url = (String) it.next();
			if (isLive(url))
				this.allLiveURLs.add(url);
		}
	}

	private boolean isLive(String url) {
		for (int i = 0; i < 3;) {
			try {
				HttpURLConnection urlconn = (HttpURLConnection) new URL(url)
						.openConnection();
				int code = urlconn.getResponseCode();
				if (code == 200)
					return true;
			} catch (Exception localException) {
				i++;
			}

		}

		return false;
	}
}